package com.wangshuqi.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountListener implements HttpSessionListener {


    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("sessionCreated ......");
        ServletContext servletContext = se.getSession().getServletContext();
        Integer online = (Integer) servletContext.getAttribute("online");
        if (online == null) {
            online = 1;
        } else {
            online = online + 1;
        }
        servletContext.setAttribute("online", online);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("sessionDestroyed ......");
        ServletContext servletContext = se.getSession().getServletContext();
        Integer online = (Integer) servletContext.getAttribute("online");
        servletContext.setAttribute("online", online - 1);
    }
}
